//
// Created by Luhang on 2023/5/10/010.
//

#ifndef INC_0313_PRIORITYQUEUE_H
#define INC_0313_PRIORITYQUEUE_H

#endif //INC_0313_PRIORITYQUEUE_H

#include <iostream>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;

namespace Lu{
    template<class T>
    struct less{
        bool operator()(const T& x, const T& y){
            return x < y;
        }
    };

    template<class T>
    struct greater{
        bool operator()(const T& x, const T& y){
            return x > y;
        }
    };

    template<class T, class Container = std::vector<T>, class Compare = less<T>>
    class priority_queue{
    public:
        void adjust_up(int child){
            Compare com;
            int parent = (child - 1) / 2;
            while (child > 0){
                if (Compare()(_con[parent], _con[child])){
                //if (com(_con[parent], _con[child])){
                //if (_con[child] > _con[parent]){
                    std::swap(_con[child], _con[parent]);
                    child = parent;
                    parent = (child - 1) / 2;
                } else
                    break;
            }
        }

        void adjust_down(int parent){
            Compare com;
            size_t child = parent * 2 + 1;
            while (child < _con.size()){
                if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))
                //if (child + 1 < _con.size() && _con[child + 1] > _con[child])
                    ++child;
                if (com(_con[parent], _con[child])){
                //if (_con[child] > _con[parent]){
                    std::swap(_con[child], _con[parent]);
                    parent = child;
                    child = parent * 2 + 1;
                } else
                    break;
            }
        }

        void push(const T& x){
            _con.push_back(x);
            adjust_up(_con.size() - 1);
        }

        void pop(){
            std::swap(_con[0], _con[_con.size() - 1]);
            _con.pop_back();
            adjust_down(0);
        }

        const T& top(){
            return _con[0];
        }

        size_t size(){
            return _con.size();
        }

        bool empty(){
            return _con.empty();
        }

    private:
        Container _con;
    };

    void test_priority_queue1(){
        priority_queue<int, std::vector<int>, greater<int>> pq;
        pq.push(2);
        pq.push(9);
        pq.push(6);
        pq.push(8);
        pq.push(5);
        while (!pq.empty()){
            cout << pq.top() << " ";
            pq.pop();
        }
        cout << endl;
    }

    class Date
    {
    public:
        Date(int year = 1900, int month = 1, int day = 1)
                : _year(year)
                , _month(month)
                , _day(day)
        {}

        bool operator<(const Date& d)const
        {
            return (_year < d._year) ||
                   (_year == d._year && _month < d._month) ||
                   (_year == d._year && _month == d._month && _day < d._day);
        }

        bool operator>(const Date& d)const
        {
            return (_year > d._year) ||
                   (_year == d._year && _month > d._month) ||
                   (_year == d._year && _month == d._month && _day > d._day);
        }

        friend std::ostream& operator<<(std::ostream& _cout, const Date& d)
        {
            _cout << d._year << "-" << d._month << "-" << d._day;
            return _cout;
        }

    private:
        int _year;
        int _month;
        int _day;
    };

    class PDataLess{
    public:
        bool operator()(const Date* p1, const Date* p2){
            return *p1 < *p2;
        }
    };

    class PDataGreat{
    public:
        bool operator()(const Date* p1, const Date* p2){
            return *p1 < *p2;
        }
    };

    void test_priority_queue2(){
        priority_queue<Date, std::vector<Date>, greater<Date>> q1;
        q1.push(Date(2016, 10, 1));
        q1.push(Date(2020, 8, 1));
        q1.push(Date(2017, 9, 25));
        cout << q1.top() << endl;

        priority_queue<Date*, std::vector<Date*>, PDataLess> q2;
        q2.push(new Date(2016, 10,1));
        q2.push(new Date(2020, 8,1));
        q2.push(new Date(2017, 9,25));
        cout << *(q2.top()) << endl;
    }
}